package labb2;

import java.util.*;

class FifteenModel implements Boardgame{
	private String[][] board = new String[4][4];
	private String currentMessage = "";
	private int iempt, jempt;
	private Random irand = new Random();
	private Random jrand = new Random();
        private int irandtemp;
        // test
        //test2
        private int jrandtemp;
	boolean turn;
	public boolean move(int i, int j){
		boolean done;
		if (i > 3 || i < 0 || j > 3 || j < 0){
			currentMessage = "Välj en ruta inom spelplanen!";
			done = false;
		}
		else if ((i == iempt && (j == jempt + 1 || j == jempt - 1)) || (j == jempt && (i == iempt + 1 || i == iempt - 1))){
			board[iempt][jempt] = board[i][j];
			board[i][j] = null;
			iempt = i;
			jempt = j;
			currentMessage = "OK!";			
			done = true;

		}
		else {
			if (board[i][j] == null){
				currentMessage = "Välj en annan ruta än den tomma!";
			}
			else{
				currentMessage = "Välj en ruta brevid den tomma!";
			}
			done = false;
		}
		return done;
	}
	public String getStatus(int i, int j){
		String status = board [i][j];
		return status;
	}
	public String getMessage(){
		return currentMessage;
	}
	public FifteenModel(){
		int n = 1;
		String m = Integer.toString(n);
		outerloop:
		// lägger ut brickorna 	
		for (int k = 0; k < 4; k++){
			for (int l = 0; l < 4; l++){
				board [k][l] = m;
				n++;
				m = Integer.toString(n);
				if (n == 16){
					iempt = 3;
					jempt = 3;
					break outerloop;
				}
			}
		}
		// shufflar runt brickorna
		for (int i = 0; i < 100; i++){
                    irandtemp = irand.nextInt(3)-1;
                    move(iempt+irandtemp, jempt); 
                    jrandtemp = jrand.nextInt(3)-1;
                    move(iempt, jempt+jrandtemp);
                    currentMessage = "Välkommen till femtonspelet!";
                        
                    }
		}
}
